//
//  PersonVC.m
//  AppNew
//
//  Created by 华伟 on 2019/4/4.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "PersonVC.h"

@interface PersonVC ()

@end

@implementation PersonVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    
}


- (IBAction)headImgAction:(id)sender {
    
    //提示选择拍照还是选择照片
    [self createAlertPhotoView];
    
}

//提示选择拍照还是选择照片
- (void)createAlertPhotoView{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照"
                                                              style:UIAlertActionStyleDefault
                                                            handler:^(UIAlertAction * _Nonnull action)
                                      {
                                          BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
                                          if (!isCamera) {
                                              NSLog(@"没有摄像头");
                                              return;
                                          }else {
                                              UIImagePickerController *imgPickerC = [[UIImagePickerController alloc] init];
                                              
                                              imgPickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
                                              imgPickerC.delegate = self;
                                              [self presentViewController:imgPickerC animated:YES completion:nil];
                                          }
                                          
                                      }];
    
    [alertController addAction:takePhotoAction];
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册中选取"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action)
                                  {
                                      UIImagePickerController *imgPickerC = [[UIImagePickerController alloc] init];
                                      
                                      
                                      imgPickerC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                                      imgPickerC.delegate = self;
                                      [self presentViewController:imgPickerC animated:YES completion:nil];
                                      
                                      
                                  }];
    
    [alertController addAction:photoAction];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             [alertController dismissViewControllerAnimated:YES
                                                                                                 completion:nil];
                                                         }];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController
                       animated:YES
                     completion:nil];
}


- (void)uploadPicwithFile{
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    NSString *nameStr = [NSString stringWithFormat:@"fileName.PNG"];
    NSData *imageData = UIImageJPEGRepresentation(_iconImg, 0.1);
//    NSData *imageData = UIImagePNGRepresentation(_iconImg);
    [dataDic setObject:imageData forKey:nameStr];
    
    
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/userOpt/uploadPicImg"];
    [NetRequestTool spePost:url
                  params:nil
                    data:dataDic
                 success:^(id result) {
                     
                     NSLog(@"%@",result);
                     BOOL resultFlag = [[result objectForKey:@"flag"] boolValue];
                     if (resultFlag) {
                         _filePath = result[@"data"];
                     }else{
                         [SVProgressHUD showErrorWithStatus:@"上传失败"];
                     }
                 } failure:^(NSError *error) {
                     
                     NSLog(@"%@",error);
//                     [self hideFailHUD:@"上传失败"];
                     
                 }];

    
//    [ZSTools post:url
//           params:nil
//             data:dataDic
//          success:^(id result) {
//              NSLogZS(@"返回数据:%@",result);
//              BOOL resultFlag = [[result objectForKey:@"flag"] boolValue];
//              if (resultFlag) {
//                  _filePath = result[@"data"][@"filePath"];
//                  [self editUserInfo];
//              }else{
//                  [self hideFailHUD:@"上传失败"];
//              }
//          } failure:^(NSError *error) {
//
//              [self hideFailHUD:@"上传失败"];
//
//          }];
    
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    
    if ([mediaType isEqualToString:@"public.image"]) {
        //取得照片
        UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
        _iconImg = originalImage;
        [self uploadPicwithFile];
        //        [_tableView reloadData];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
